package bday.tvlport.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import bday.tvlport.scheduler.ApplicationStartup;
import bday.tvlport.utils.LoggerUtility;

public class SendMailTLS {

	public static void mail(String to, String text, boolean isSupportMail) {
		String cc = null;
		String subject = null;
		if(isSupportMail) {
			to = ApplicationStartup.getMailSupport();
			subject = "Birthday Support";
		} else {
			cc = ApplicationStartup.getMailCopy();
			subject = "Birthday Notification";
		}
		final String username = "arunasishghosh@gmail.com";
		final String password = "ulrqvvgtdfivvybh";

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		//props.put("mail.smtp.user", "arunasishghosh");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.EnableSSL.enable", "true");

		props.setProperty("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.socketFactory.fallbac k", "false");
		props.setProperty("mail.smtp.port", "465");
		props.setProperty("mail.smtp.socketFactory.port", "465");

		// props.put("mail.smtp.user","Your username");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "25");
		// props.put("mail.debug", "true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.EnableSSL.enable", "true");

		props.setProperty("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.socketFactory.fallbac k", "false");
		props.setProperty("mail.smtp.port", "465");
		props.setProperty("mail.smtp.socketFactory.port", "465");

		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("BirthdayNotification@travelport.com"));
			//message.setReplyTo(InternetAddress.parse("arunasishghosh@gmail.com"));
			try {
				message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
				message.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc));
			} catch (Exception e) {
			}
			message.setSubject(subject);
			message.setText(text);

			Transport.send(message);

			LoggerUtility.info("Mail sent to: " + to);

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}